<template>
  <transition name="el-zoom-in-center">
    <div class="cu-preview-main">
      <div class="cu-common-page-header">
        <el-page-header @back="goBack" content="合同记录" />
        <div class="options">
          <el-button @click="goBack()">{{ $t('common.cancelButton') }}</el-button>
        </div>
      </div>
      <div class="main">
        <div class="cu-common-head">
          <div>
            <el-button type="primary" icon="el-icon-plus" @click="addOrUpdateHandle(query.campusId, 'add')" v-has="'btn_add'">新增</el-button>
          </div>
          <div class="cu-common-head-right">
            <el-tooltip effect="dark" content="刷新" placement="top">
              <el-link icon="icon-ym icon-ym-Refresh cu-common-head-icon" :underline="false" @click="reset()" />
            </el-tooltip>
          </div>
        </div>
        <custom-table v-loading="listLoading" :data="list" @selection-change="handleSelectionChange">
          <el-table-column label="合同类型" prop="type" align="center" v-if="custom.hasP('type')" width="80">
            <template slot-scope="scope">
              {{ scope.row.type | dynamicCodeText(typeOptions) }}
            </template>
          </el-table-column>
          <el-table-column label="开始日期" prop="startDate" align="center" v-if="custom.hasP('startDate')" :formatter="custom.tableDateFormat" width="100" />
          <el-table-column label="截止日期" prop="endDate" align="center" v-if="custom.hasP('endDate')" :formatter="custom.tableDateFormat" width="100" />

          <el-table-column label="应收金额" prop="receivable" align="right" v-if="custom.hasP('receivable')" width="80" />
          <el-table-column label="实收金额" prop="actual" align="right" v-if="custom.hasP('actual')" width="80" />
          <el-table-column label="支付情况" prop="isPayed" align="left" v-if="custom.hasP('isPayed')" width="80">
            <template slot-scope="scope">
              {{ scope.row.isPayed | dynamicCodeText(isPayedOptions) }}
            </template>
          </el-table-column>
          <el-table-column label="政策" prop="policy" align="left" v-if="custom.hasP('policy')" show-overflow-tooltip />
          <el-table-column label="创建用户" prop="createUserName" align="left" v-if="custom.hasP('createUserName')" width="80" show-overflow-tooltip />
          <el-table-column label="创建时间" prop="createTime" align="center" v-if="custom.hasP('createTime')" :formatter="custom.tableDateTimeFormat" width="120" />
          <el-table-column label="修改用户" prop="updateUserName" v-if="custom.hasP('updateUserName')" width="80" show-overflow-tooltip />
          <el-table-column label="修改时间" prop="updateTime" align="left" v-if="custom.hasP('updateTime')" :formatter="custom.tableDateTimeFormat" width="120" />
          <el-table-column label="操作" fixed="right" width="100" align="center">
            <template slot-scope="scope">
              <el-button type="text" @click="addOrUpdateHandle(scope.row.id)">缴费</el-button>
              <el-button type="text" size="mini" @click="handleDel(scope.row.id)" class="cu-table-delBtn" v-has="'btn_remove'">删除</el-button>
            </template>
          </el-table-column>
        </custom-table>
        <pagination :total="total" :page.sync="listQuery.currentPage" :limit.sync="listQuery.pageSize" @pagination="initData" />
        <custom-form v-if="formVisible" ref="CustomForm" @refresh="refresh" />
      </div>
    </div>
  </transition>
</template>
<script>
import request from '@/utils/request'
import { getDictionaryDataSelector } from '@/api/systemData/dictionary'
import CustomForm from './Form'
export default {
  components: { CustomForm },
  data() {
    return {
      query: {
        campusId: undefined,
        brand: 'bell',
        isPayed: undefined
      },
      list: [],
      listLoading: true,
      multipleSelection: [],
      total: 0,
      listQuery: {
        currentPage: 1,
        pageSize: 20,
        sort: 'desc',
        sidx: 'createTime'
      },
      formVisible: false,
      exportBoxVisible: false,
      columnList: [{ prop: 'campusId', label: '校区' }, { prop: 'startDate', label: '开始日期' }, { prop: 'endDate', label: '截止日期' }, { prop: 'receivable', label: '应收金额' }, { prop: 'actual', label: '实收金额' }, { prop: 'policy', label: '政策' }, { prop: 'createUserName', label: '创建用户' }, { prop: 'createTime', label: '创建日期' }],
      isPayedOptions: [],
      typeOptions: []
    }
  },
  computed: {},
  created() {
    this.getIsPayedOptions() 
    this.getTypeOptions()
    this.getBrandOptionsOptions()
    this.getCampusTypeOptions()
    this.getCampusStatusOptions()
  },
  methods: {
    getIsPayedOptions() {
      getDictionaryDataSelector('PayStatus').then(res => {
        this.isPayedOptions = res.data.list
      })
    },
    getTypeOptions() {
      getDictionaryDataSelector('SignType').then(res => {
        this.typeOptions = res.data.list
      })
    },
    initData(id) {
      this.query.campusId = id || ''
      this.listLoading = true
      let _query = {
        ...this.listQuery,
        ...this.query
      }
      let query = {}
      for (let key in _query) {
        if (Array.isArray(_query[key])) {
          query[key] = _query[key].join()
        } else {
          query[key] = _query[key]
        }
      }
      request({
        url: `/api/campus/contract`,
        method: 'GET',
        data: query
      }).then(res => {
        this.list = res.data.list
        this.total = res.data.pagination.total
        this.listLoading = false
      })
    },
    handleDel(id) {
      this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
        type: 'warning'
      })
        .then(() => {
          request({
            url: `/api/campus/contract/${id}`,
            method: 'DELETE'
          }).then(res => {
            this.$message({
              type: 'success',
              message: res.msg,
              onClose: () => {
                this.initData(this.query.campusId)
              }
            })
          })
        })
        .catch(() => {})
    },
    handleSelectionChange(val) {
      const res = val.map(item => item.id)
      this.multipleSelection = res
    },
    handleBatchRemoveDel() {
      if (!this.multipleSelection.length) {
        this.$message({
          type: 'error',
          message: '请选择一条数据',
          duration: 1500
        })
        return
      }
      const ids = this.multipleSelection
      this.$confirm('您确定要删除这些数据吗, 是否继续？', '提示', {
        type: 'warning'
      })
        .then(() => {
          request({
            url: `/api/system/contract/batchRemove`,
            method: 'POST',
            data: ids
          }).then(res => {
            this.$message({
              type: 'success',
              message: res.msg,
              onClose: () => {
                this.initData(this.query.campusId)
              }
            })
          })
        })
        .catch(() => {})
    },
    addOrUpdateHandle(row, isDetail) {
      this.formVisible = true
      this.$nextTick(() => {
        this.$refs.CustomForm.init(row, isDetail)
      })
    },
    exportData() {
      this.exportBoxVisible = true
      this.$nextTick(() => {
        this.$refs.ExportBox.init(this.columnList)
      })
    },
    download(data) {
      let query = { ...data, ...this.listQuery, ...this.query }
      request({
        url: `/api/system/contract/Actions/Export`,
        method: 'GET',
        data: query
      }).then(res => {
        if (!res.data.url) return
        window.location.href = this.define.comUrl + res.data.url
        this.$refs.ExportBox.visible = false
        this.exportBoxVisible = false
      })
    },
    search() {
      this.listQuery = {
        currentPage: 1,
        pageSize: 20,
        sort: 'desc',
        sidx: 'createTime'
      }
      this.initData(this.query.campusId)
    },
    refresh(isrRefresh) {
      this.formVisible = false
      if (isrRefresh) this.search()
    },
    reset() {
      for (let key in this.query) {
        this.query[key] = undefined
      }
      this.listQuery = {
        currentPage: 1,
        pageSize: 20,
        sort: 'desc',
        sidx: 'createTime'
      }
      this.initData()
    },
    goBack() {
      this.$emit('close')
    }
  }
}
</script>
<style lang="scss" scoped>
.main {
  overflow: hidden;
  display: flex;
  flex-direction: column;
  padding: 0 0 10px;
  >>> .el-table {
    flex: 1;
    border-top: none;
  }
}
</style>
